﻿using System;
using UnityEngine;
using UnityEngine.InputSystem;

namespace DL.Input
{

    public class CharacterInputSystem : Singleton<CharacterInputSystem>
    {
        private InputController _inputController;
        public InputController.PlayerInputActions _playerInputActions { get; private set; }

        [HideInInspector] public bool playerControllerInputBlocked = false; //玩家输入控制锁定

        private bool m_ExternalInputBlocked = false;//外部输入控制锁定
        //内部函数
        protected override void Construct()
        {
            if (_inputController == null)
                _inputController = new InputController();
            _playerInputActions = _inputController.PlayerInput;
        }

        private void OnEnable()
        {
            _inputController.Enable();
        }

        private void OnDisable()
        {
            _inputController.Disable();
        }

        private void Update()
        {
            if (Cursor.lockState == CursorLockMode.None)
            {
                ReleaseControl();
            }
            else
            {
                GainControl();
            }
        }

        //Key Setting
        /// <summary>
        /// wasd输入值，归一化过
        /// </summary>
        public Vector2 Movement => !m_ExternalInputBlocked && !playerControllerInputBlocked
            ? _playerInputActions.Movement.ReadValue<Vector2>()
            : default;

        public Vector2 CameraLook => !m_ExternalInputBlocked && !playerControllerInputBlocked
            ? _playerInputActions.CameraLook.ReadValue<Vector2>()
            : default;

        public bool ChangeMovementMode => _playerInputActions.LeftControl.triggered && !m_ExternalInputBlocked &&
                                          !playerControllerInputBlocked;

        public bool Dash => _playerInputActions.LeftShift.triggered && !m_ExternalInputBlocked &&
                            !playerControllerInputBlocked;


        public bool L_Atk =>
            _playerInputActions.L_Atk.triggered && !m_ExternalInputBlocked && !playerControllerInputBlocked;

        public bool R_Atk =>
            _playerInputActions.R_Atk.triggered && !m_ExternalInputBlocked && !playerControllerInputBlocked;

        public bool Jump => _playerInputActions.Space.triggered && !m_ExternalInputBlocked &&
                            !playerControllerInputBlocked;

        public bool E => _playerInputActions.E.triggered && !m_ExternalInputBlocked &&
                                   !playerControllerInputBlocked;

        public bool Q =>
            _playerInputActions.Q.triggered && !m_ExternalInputBlocked && !playerControllerInputBlocked;

        public bool R =>
            _playerInputActions.R.triggered && !m_ExternalInputBlocked && !playerControllerInputBlocked;

        public bool HaveControl()
        {
            return !m_ExternalInputBlocked;
        }

        public void ReleaseControl()
        {
            m_ExternalInputBlocked = true;
        }

        public void GainControl()
        {
            m_ExternalInputBlocked = false;
        }
    }
}